package com.xigua.xcar.widgets.phoneIndexList;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xigua.xcar.R;
import com.xigua.xcar.data.PhoneIndexListData;

public class PhoneIndexListAdapter extends BaseAdapter {
	private static final String tag = "PhoneIndexListAdapter";
	private ArrayList<PhoneIndexListData> mArray = null;
	private LayoutInflater mInflater = null;

	public PhoneIndexListAdapter(Context context) {
		Log.i(tag, "PhoneIndexListAdapter");
		if (null != context) {
			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
	}

	public void setData(ArrayList<PhoneIndexListData> array) {
		mArray = array;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mArray.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mArray.get((position) % mArray.size());
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	private void updateView(View convertView, PhoneIndexListData data, int index) {
		TextView txt = null;
		txt = (TextView) convertView.findViewById(R.id.txt_phoneindex_content);
		txt.setText(data.getContent());
		convertView.setTag(data);
		if (data.getType() == 1) {
			convertView.findViewById(R.id.image_phoneindex_image)
					.setVisibility(View.GONE);
		} else {
			convertView.findViewById(R.id.image_phoneindex_image)
					.setVisibility(View.VISIBLE);
		}
		if(0 == index){
			convertView.setBackgroundResource(R.drawable.list_back_top);
		}else if((mArray.size()-1) == index){
			convertView.setBackgroundResource(R.drawable.list_back_bottom);
		}else{
			convertView.setBackgroundResource(R.drawable.list_back_middle);
		}
		if(mArray.size()==1){
			convertView.setBackgroundResource(R.drawable.about_back);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		position = position % (mArray.size());
		PhoneIndexListData data = mArray.get(position);
		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.phoneindex_item, null);
		}
		updateView(convertView, data,position);
		return convertView;
	}

}
